package com.hk.admin.spring.advice;

import com.hk.admin.common.base.dto.message.Message;
import com.hk.admin.common.base.dto.message.MessageErrorCode;
import com.hk.admin.common.exception.HKAdminException;
import com.hk.admin.common.util.MessageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <br>
 * <b>功能：</b>全局异常处理<br>
 * <b>作者：</b>Pan.ShiJu<br>
 * <b>日期：</b>2017/4/11 23:27<br>
 */
@ControllerAdvice
public class ExceptionHandlerAdvice {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 全局异常捕捉处理
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<Message> errorHandler(Exception ex) {
        logger.error(ex.getMessage());
        ex.printStackTrace();
        return MessageUtil.errorInternalServer(MessageErrorCode.ERROR_HK_ADMIN_MSG);
    }

    /**
     * 拦截捕捉自定义异常
     */
    @ResponseBody
    @ExceptionHandler(value = HKAdminException.class)
    public ResponseEntity<Message> myErrorHandler(HKAdminException ex) {
        logger.error(ex.getMessage());
        return ex.getResponseEntity();
    }


}
